//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef _STATE_CONTROLLER_YG_H__
#define _STATE_CONTROLLER_YG_H__

#include "def.h"

class CStateController
{
public:
								CStateController(class CGUILogic& logic);
								~CStateController(void);


	void						OnCMD(const COMMANDRESPONSE& cmd, RTSTRING & strScipt, bool bEnabled, bool& bForced);

	bool						HaveSwap()							{ return m_bSwap; }

	void						Reset();

	void						SetConntractRecordsCount(size_t stRecords);

	void						SetEnableNATRepair( bool bEnable )	{ m_bEnableRepair = bEnable; }

	bool						IsChanged()							{ return m_bChanged; }

	void						OnFlushAction(cPanelType cTp);
	void						OnRebootAction(cPanelType cTp);


	std::string					BuildStateXML();

	size_t						GetCurRTCacheSize() { return m_stRT.stCur; }
	


protected:
	void						OnVersion(const RTSTRING& strData);
	void						OnMemory(const COMMANDRESPONSE& cmd);
	void						OnConntracMax( const COMMANDRESPONSE& cmd );
	void						OnRTCacheMax( const COMMANDRESPONSE& cmd );
	void						OnSlabTable( const COMMANDRESPONSE& cmd );

	void						DoProcess(RTSTRING & strScipt, bool bEnabled);
	void						DoStartUpActions(RTSTRING & strScipt);

	void						CheckNATCacheUsage(RTSTRING & strScipt);

private:
	typedef struct tagMEMState
	{
		tagMEMState():stMemTotal(0), stMemFree(0), tBootCount(0) {}
		size_t					stMemTotal;
		size_t					stMemFree;
		bool					IsValid() const { return stMemTotal && stMemFree; }

		size_t					tBootCount;

	}MEMState;

	typedef struct tagTableState
	{
		tagTableState():stAllocated(0), stTotalMAX(0), stCur(0), stObjperslab(0), stPagesperslab(0), stobjsize(0), stTotal(0)
		{
			tChange = 0;

			tSizeChangeCount = 0;
			tRepairCount = 0;
			tBootCount = 0;
			nSizeDelta = 0;
		}

		size_t					stAllocated;
		size_t					stCur;
		size_t					stObjperslab;
		size_t					stPagesperslab;
		size_t					stobjsize;
		time_t					tChange;

		size_t					tSizeChangeCount;
		size_t					tRepairCount;
		size_t					tBootCount;

		int						nSizeDelta;

		bool					IsValid() const { return stAllocated && stTotalMAX && stTotal&& stCur && stObjperslab && stPagesperslab && stobjsize; }

		void					UpdateChangeTime() { tChange = time(NULL); }

		void					SetTotal( size_t stV )
		{
			stTotal = stV;

			if ( stTotal>stTotalMAX )
			{
				stTotalMAX = stTotal;
			}
		}

		size_t					GetMaxTotal() const		{ return stTotalMAX;	}
		size_t					GetTotal() const		{ return stTotal;		}

		size_t					GetMemNotAllocated() const;

	protected:
		size_t					stTotalMAX;
		size_t					stTotal;


	}TableState;

	size_t						GetUsage( const TableState& tb );
	size_t						GetMaxPossible( const TableState& tb, const TableState& tbOther, double fMult=0.5 );
	size_t						SlabOptimize( const TableState& tb, size_t stWanted );



private:
	MEMState					m_stMEM;
	TableState					m_stNAT;
	TableState					m_stRT;

	bool						m_bSwap;

	bool						m_bSentStartUpValues;

	bool						m_bIs2_4Kernel;
	bool						m_bIs2_2Kernel;
	bool						m_bIs2_6Kernel;

	size_t						m_stNATRealUsed;
	size_t						m_stRTRealUsed;

	bool						m_bEnableRepair;
	size_t						m_nTryCount;
	class CGUILogic&			m_logic;

	bool						m_bChanged;

	size_t						m_tCPUBootCount;
	size_t						m_tCPULoad;
	size_t						m_tCPUFrme;
};

#endif //_STATE_CONTROLLER_YG_H__